package cs304.p3.library.model;

import java.awt.Point;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.table.AbstractTableModel;

@SuppressWarnings("serial")
public class DBTableModel extends AbstractTableModel{
	
	public static enum TableType {
		BORROWER("BID", "Password", "Name", "Address", "Phone",  "emailAddress", "sinOrStNo", "ExpiryDate", "Type"),
		BORROWERTYPE("Type", "BookTimeLimit"),
		BOOK("CallNumber", "ISBN", "Title", "MainAuthor", "Publisher", "Year"),
		HASAUTHOR("CallNumber", "Name"),
		HASSUBJECT("CallNumber", "Subject"),
		BOOKCOPY("CallNumber", "CopyNo", "Status"),
		HOLDREQUEST("HID", "BID", "CallNumer", "IssuedDate"),
		BORROWING("BORID", "BID", "CallNumber", "CopyNo", "OutDate", "InDate"),
		FINE("FID", "Amount", "IssuedDate", "PaidDate", "BORID");
		
		private String[] labels;
		
		private TableType(String... labels){
			this.labels = labels;
		}

		public String[] getLabels(){
			return labels;
		}
		
		public int getCount(){
			return labels.length;
		}
	}
	
	private String[] column_names;
	private int rows;
	private final int columns;
	private Map<Point, String> data;
	private TableType tableType;
	
	public DBTableModel(TableType type){
		column_names = type.getLabels();
		rows = 0;
		columns = column_names.length;
		data = new HashMap<Point, String>();
		tableType = type;
	}

	@Override
	public int getColumnCount() {
		return columns;
	}
	
	@Override
	public String getColumnName(int col) {
	    return column_names[col];
	}

	@Override
	public int getRowCount() {
		return rows;
	}

	@Override
	public Object getValueAt(int row, int column) {
		return data.get(new Point(row, column));
	}
	
	public TableType getType(){
		return tableType;
	}
	
	public boolean insertData(List<String> args){
		if(args.size() == column_names.length){
			for(int i=0; i<column_names.length; i++){
				data.put(new Point(rows, i), args.get(i));
			}
			rows++;
			fireTableDataChanged();
			return true;
		}
		return false;
	}
	
	public boolean insertData(String[] args){
		if(args.length == column_names.length){
			for(int i=0; i<column_names.length; i++){
				data.put(new Point(rows, i), args[i]);
			}
			rows++;
			fireTableDataChanged();
			return true;
		}
		return false;
	}

	public boolean deleteData(String[] args){
		//TODO
		return true;
	}

}
